<template>
<div class="product-add mt30">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <el-form-item :label="$t('plus.distributor_condition')">
        <div>
          <el-radio v-model="form.become" :label="10">{{$t('plus.require_review')}}</el-radio>
          <el-radio v-model="form.become" :label="20">{{$t('plus.no_review_needed')}}</el-radio>
        </div>
      </el-form-item>

      <el-form-item :label="$t('plus.become_by_purchase')">
        <el-radio v-model="form.becomeBuyProduct" :label="0">{{$t('plus.disable')}}</el-radio>
        <el-radio v-model="form.becomeBuyProduct" :label="1">{{$t('plus.enable')}}</el-radio>
        <div class="tips">{{$t('plus.auto_become_note')}}</div>
        <template v-if="form.becomeBuyProduct == 1">
          <div class="ww100">
            <el-button type="primary" @click="openProduct" class="mb16"
              >{{$t('plus.select_product')}}</el-button
            >
          </div>
          <el-row
            v-if="form.productImage && form.productImage.length > 0"
            class="ww100"
          >
            <div
              v-for="(item, index) in form.productImage"
              :key="index"
              class="imgItem pr mr10"
            >
              <img v-bind:src="item.image" width="100" height="100" />
              <a
                href="javascript:void(0)"
                class="delete-btn"
                @click="deleteFunc(index)"
              >
                <el-icon>
                  <CircleCloseFilled />
                </el-icon>
              </a>
              <p class="text-ellipsis">{{item.productName}}</p>
            </div>
          </el-row>
        </template>
      </el-form-item>

      <el-form-item :label="$t('plus.become_downline_condition')">
        <div>
          <el-radio v-model="form.downline" :label="10"
            >{{$t('plus.first_click_link')}}</el-radio
          >
        </div>
      </el-form-item>

      <!--提交-->
      <div class="common-button-wrapper">
        <el-button
          size="small"
          type="primary"
          @click="onSubmit"
          :loading="loading"
          >{{$t('plus.submit')}}</el-button
        >
      </div>
    </el-form>
    <!--产品列表弹出层组件-->
    <Product :isProduct="isProduct" @closeDialog="closeDialogFunc($event)"
      >{{$t('plus.product_popup')}}</Product
    >
  </div>
</template>

<script>
import PlusApi from "@/api/plus.js";
import Product from "@/components/product/Product.vue";
export default {
  components: {
    /*产品列表组件*/
    Product: Product
  },
  data() {
    return {
      /*切换菜单*/
      // activeIndex: '1',
      /*form表单数据*/
      form: {},
      /*是否打开产品弹出层*/
      isProduct: false,
      loading: false
    };
  },
  props: {
    settingData: Object
  },
  created() {
    this.form = this.settingData.condition;
    if (!this.form.productImage) {
      this.form.productImage = [];
    }
  },
  methods: {
    /*提交表单*/
    onSubmit() {
      let self = this;
      self.loading = true;
      let params = this.form;
      PlusApi.condition(params, true).then(data => {
        self.loading = false;
        ElMessage({
          message: $t("plus.setting_success"),
          type: "success"
        });
      }).catch(error => {
        self.loading = false;
      });
    },
    /*删除商品*/
    deleteFunc(i) {
      this.form.becomeBuyProductIds.splice(i, 1);
      this.form.productImage.splice(i, 1);
    },
    /*产品列表弹出层*/
    openProduct() {
      this.isProduct = true;
      console.log(this.isProduct);
    },
    /*关闭弹窗*/
    closeDialogFunc(e) {
      this.isProduct = e.openDialog;
      if (e.type == "success") {
        if (this.form.becomeBuyProductIds.indexOf(e.params.productId) == -1) {
          this.form.becomeBuyProductIds.push(e.params.productId);
          this.form.productImage.push({
            productId: e.params.productId,
            image: e.params.image,
            productName: e.params.productName
          });
        } else {
          ElMessage({
            message: $t("plus.product_selected"),
            type: "warning"
          });
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.tips {
  color: #ccc;
}

.img {
  width: 100px;
  margin-top: 10px;
  height: 130px;
  margin-right: 10px;
}

.img img {
  border: 1px solid #eeeeee;
}

.delete-btn {
  position: absolute;
  display: block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  right: -8px;
  top: -8px;
  color: red;
}
</style>

